<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: Circle.php
 * Date: 2017/1/2
 * Time: 23:13
 */
namespace app\api\model\dao;

use think\Model;
use think\Db;

class MsgSet extends Model
{
    protected $table = 'msg_set';

    protected $pk = 'id';

    /**
     *
     *
     */
    public function add($arrInput)
    {
        unset($arrInput[$this->pk]);

        $this->data($arrInput);
        $this->save();
        return $this->data[$this->pk];
    }

    /**
     *
     *
     */
    public function upd($userid, $input)
    {

        $conds = [
            'user_id' => ['=', $userid]
        ];

        unset($input['id']);
        unset($input['token']);
        return Db::table($this->table)->where($conds)->update($input);
    }

    /**
     *
     *
     */
    public function getByUserid($userid, $fields = [])
    {
        $this->allowField(true);

        $arrCond = [
            'user_id' => ['=', $userid]
        ];

        $arrField = [];
        foreach ($fields as $val) {
            if (in_array($val, $this->field)) {
                $arrField[] = $val;
            }
        }
        empty($arrField) && $arrField = $this->field;

        $ret = Db::table($this->table)->where($arrCond)->field($arrField)->select();
        if (false === $ret) {
            return false;
        }
        if (isset($ret[0])) {
            return $ret[0];
        } else {
            return [];
        }
    }

}
